package com.gzyj.web.utils;

import io.netty.util.internal.StringUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtils extends org.apache.commons.lang3.time.DateUtils{
    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    public static int secToNowTime(Date times) {
        Date date2 = new Date();
        Date date1 = times;
        int secs = 0;
        if (date1 != null && date2 != null) {
            secs = (int) ((date2.getTime() - date1.getTime()) / (1000));
        }
        return secs < 0 ? 1 : secs;


    }
    public static String unitFormat(int i) {
        String retStr = null;
        if (i >= 0 && i < 10) {
            retStr = "0" + Integer.toString(i);
        } else {
            retStr = "" + i;
        }
        return retStr;
    }
    public static String secToTime(int time) {
        String timeStr = null;
        int day = 0;
        int hour = 0;
        int minute = 0;
        int second = 0;
        if (time <= 0) {
            return "00分00秒";
        } else {
            minute = time / 60;
            if (minute < 60) {
                second = time % 60;
                timeStr = unitFormat(minute) + "分" + unitFormat(second) + "秒";
            } else {
                hour = minute / 60;
                day = hour / 24;
                if (day < 1) {

                    minute = minute % 60;
                    second = time - hour * 3600 - minute * 60;
                    timeStr = unitFormat(hour) + "时" + unitFormat(minute) + "分" + unitFormat(second) + "秒";
                } else {
                    hour = hour % 24;
                    minute = minute % 60;
                    second = time - day * 3600 * 24 - hour * 3600 - minute * 60;
                    timeStr = day + "天" + unitFormat(hour) + "时" + unitFormat(minute) + "分" + unitFormat(second) + "秒";
                }
            }
        }
        return timeStr;
    }

    public static int secStrToNowTime(String times) {
        try {
            if (StringUtil.isNullOrEmpty(times)) {
                return 0;
            }
            Date date2 = new Date();
            Date date1 = dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, times);
            int secs = 0;
            if (date1 != null && date2 != null) {
                secs = (int) ((date2.getTime() - date1.getTime()) / (1000));
            }
            return secs < 0 ? 1 : secs;
        } catch (Exception e) {
            return 0;
        }
    }

    public static int secStrToNowTime(Date times) {
        try {

            Date date2 = new Date();
            int secs = 0;
            if (times != null && date2 != null) {
                secs = (int) ((date2.getTime() - times.getTime()) / (1000));
            }
            return secs < 0 ? 1 : secs;
        } catch (Exception e) {
            return 0;
        }
    }


    public static final Date dateTime(final String format, final String ts) {
        try {
            return new SimpleDateFormat(format).parse(ts);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }



}
